package com.itheima.test1;

import java.io.*;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class Test1 {

    public static void main(String[] args) {
        writer();
        reader();
    }

    public static void writer() {
        Scanner sc = new Scanner(System.in);
        TreeSet<Student> studentTreeSet = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return  (o2.getChinese() + o2.getMath() + o2.getEnglish()).compareTo(o1.getChinese() + o1.getMath() + o1.getEnglish());
            }
        });
        for (int i = 1; i <=3 ; i++) {
            System.out.println("请录入第"+i+"个学生的成绩");
            System.out.println("姓名：");
            String name = sc.next();
            System.out.println("语文成绩：");
            String chinese = sc.next();
            System.out.println("数学成绩：");
            String math = sc.next();
            System.out.println("英语成绩：");
            String english = sc.next();
            studentTreeSet.add(new Student(name,chinese,math,english));
        }
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Test/src/com/itheima/test1/Student.txt"));
            oos.writeObject(studentTreeSet);
            System.out.println("数据写入文件完毕");
            oos.flush();
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void reader(){
        System.out.println("文件中的数据展示如下：");
        System.out.println("姓名：" + "\t\t" + "语文成绩：" + "\t\t" + "数学成绩：" + "\t\t" + "英语成绩：" + "\t\t" );
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Test/src/com/itheima/test1/Student.txt"));
            TreeSet<Student> studentTreeSet = (TreeSet<Student>) ois.readObject();

            for (Student student : studentTreeSet) {
                System.out.println(student.getName() + "\t\t" + student.getChinese() + "\t\t" + student.getMath() + "\t\t" + student.getEnglish());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }


}


class Student implements Serializable {
    private static final long serialVersionUID = 6666662892189L;
    private String name;
    private String chinese;
    private String math;
    private String english;

    public Student() {
    }

    public Student(String name, String chinese, String math, String english) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getChinese() {
        return chinese;
    }

    public void setChinese(String chinese) {
        this.chinese = chinese;
    }

    public String getMath() {
        return math;
    }

    public void setMath(String math) {
        this.math = math;
    }

    public String getEnglish() {
        return english;
    }

    public void setEnglish(String english) {
        this.english = english;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", chinese='" + chinese + '\'' +
                ", math='" + math + '\'' +
                ", english='" + english + '\'' +
                '}';
    }

}
